package app.ui.widget

import im.mange.jetboot.bootstrap3.Bootstrap
import im.mange.jetboot.js.JsCmdFactory
import im.mange.jetboot.widget.{Spacer, InlineSpinner, ButtonPresentation, ServerSideButton}
import im.mange.jetboot.{Css, Renderable, Html}
import im.mange.jetboot.html.Span
import Css._
import jetboot._
import JsCmdFactory._
import Html._
import Bootstrap._
import net.liftweb.http.js.JsCmd

trait ExpandingToggleParent {
  def onShow: JsCmd = nothing
  def onHide: JsCmd = nothing
}

//TODO: glyphicon should be a Glyphicon
//TODO: ExpandingToggleButton?
case class ExpandingToggle(id: String, content: Renderable, glyphiconName: String, parent: ExpandingToggleParent) extends ServerSideButton {
  private var shown = false
  private val spinner = InlineSpinner(id + "Spinner", "", "/images/spinner_tiny.gif", initiallyHidden = true)

  //TODO: this blows somewhat
  //TODO: theres a widget in here somewhere ... jetpac
  def presentation = ButtonPresentation(
    div(
      span(
        span().classes(glyphicon(glyphiconName)).styles(marginRight("5px")),
        content
      ).classes(pullLeft),
      span(spinner).classes(pullRight)
    ).styles(clear(both)).render
  )

  def onClick = {
    shown = !shown
    if (shown) spinner.start & parent.onShow else parent.onHide
  }

  def stopSpinner = spinner.stop
}

